package com.EAsystem.service.Impl;

import com.EAsystem.entity.Announcement;
import com.EAsystem.entity.Class;
import com.EAsystem.entity.Major;
import com.EAsystem.mapper.ClassMapper;
import com.EAsystem.service.ClassService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Date;
import java.text.SimpleDateFormat;

@Service
public class ClassServiceImpl implements ClassService {

    @Autowired
    private ClassMapper classMapper;

    private String getCurrentTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(new Date());
    }

    @Override
    public Class findById(Integer id) {
        return classMapper.selectClassById(id);
    }

    @Override
    public PageInfo<Class> findByNamePage(String name, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Class> list = classMapper.findByNamePage(name, null, null);
        return new PageInfo<>(list);
    }

    @Override
    @Transactional
    public boolean save(Class classObj) {
        String now = getCurrentTime();
        classObj.setCreateTime(now);
        classObj.setUpdateTime(now);
        return classMapper.insertClass(classObj) > 0;
    }

    @Override
    @Transactional
    public boolean edit(Class classObj) {
        String now = getCurrentTime();
        classObj.setUpdateTime(now);
        return classMapper.updateClass(classObj) > 0;
    }

    @Override
    @Transactional
    public boolean delete(Integer id) {
        return classMapper.deleteClassById(id) > 0;
    }

    @Override
    @Transactional
    public boolean batchDelete(List<Integer> ids) {
        if (ids == null || ids.isEmpty()) {
            return false;
        }
        return classMapper.deleteClassByIds(ids) > 0;
    }



    @Override
    public PageInfo<Class> findAllByPage(int page, int size) {
        PageHelper.startPage(page, size);
        List<Class> list = classMapper.findAllByPage();
        return new PageInfo<>(list);
    }


} 